/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arraydependentstack.java;
/**
*
* @author mweya
*/
import arraydependentstack.java.Node;
import java.lang.reflect.Array;
import arraydependentstack.java.InvalidStackSizeException;
import arraydependentstack.java.StackOverflowException;
public class Stack<AnyType>{
int size = 0;
Node head = null;
Node[] stack = null;
public Stack(int size) throws InvalidStackSizeException {
if (size>0) {
stack = new Node[size];
} else {
throw new InvalidStackSizeException();
}
}
public Stack() {}
public Stack(AnyType data, int size) throws InvalidStackSizeException {
head = new Node<>(data);
if (size>0) {
} else {
throw new InvalidStackSizeException();
}
}
public int getSpace() {
int j = 0;
while (stack[j] != null) {
j = j+1;
}
return stack.length - j;
}
public void push(AnyType data) throws StackOverflowException {
if (getSpace()>0) {
int j = 0;
while (stack[j] != null) {
j = j+1;
}
stack[j] = new Node<>(data);
head = stack[j];
} else {
throw new StackOverflowException();
}
}
public AnyType pop() {
if (head == null) {
return (AnyType) "Stack's empty yo";
}
int j = 0;
while (stack[j+1]!=null) {
j = j+1;
}
return (AnyType) stack[j].getData();
}
@Override
public String toString() {
String out = "Stack:";
int j = 0;
while (j<stack.length) {
out = out+"\n"+stack[stack.length-j-1].getData();
}
return null;
}
}